package top.huntwolf.admin.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.huntwolf.admin.common.JsonResult;
import top.huntwolf.admin.pojo.User;
import top.huntwolf.admin.service.HistoryService;
import top.huntwolf.admin.service.UserService;
import top.huntwolf.admin.utils.RedisUtil;
import top.huntwolf.admin.utils.UUIDUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Api(description = "用户控制器")
@RestController
@CrossOrigin
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private RedisUtil redisUtil;

    /*微信用户登录详情*/
    @GetMapping("/login")
    public JsonResult<HashMap<String,String>> login(@RequestParam(value = "code") String code,
                                     @RequestParam(value = "rawData") String rawData,
                                     @RequestParam(value = "signature") String signature,
                                     HttpServletRequest request) {

        return JsonResult.success(userService.handleLogin(code, rawData, signature, request));
    }

    @PostMapping("/logout")
    public JsonResult logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute("token");

        return JsonResult.success(null);
    }

    @GetMapping("/searchHistoryList")
    public JsonResult<List<Object>> SearchHistoryList() {

        List<Object> contents = historyService.SearchHistory();
        return JsonResult.success(contents);

    }

    @DeleteMapping("/deleteHistory")
    public JsonResult deleteHistoryList(){

        historyService.deleteHistory();

        return JsonResult.success(null);
    }

    @DeleteMapping("/deleteHistory/{history}")
    @ApiOperation("删除指定搜索历史")
    public JsonResult deleteHistory(@PathVariable String history){

        historyService.deleteHistory(history);

        return JsonResult.success(null);
    }

    @GetMapping("/selectAll/pageNum/{pageNum}/pageSize/{pageSize}")
    public JsonResult<PageInfo<User>> selectAll(@PathVariable int pageNum, @PathVariable int pageSize){
        return JsonResult.success(userService.selectAll(pageNum,pageSize));
    }

    @PostMapping("/updateRole")
    public JsonResult updateRole(@RequestBody User user){
        userService.updateRole(user);
        return JsonResult.success(null);
    }

    @GetMapping("/getShareId/{openId}")
    public JsonResult<String> getShareId(@PathVariable String openId){
        //根据代理商openId，获取分享id
        String shareId = UUIDUtil.getUUID();
        redisUtil.set(shareId,openId);
        return JsonResult.success(shareId);
    }
}
